//
//  GZLoginState.m
//  Gunz
//
//  Created by Joggy on 16/3/3.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "GZLoginState.h"
#import "GZFilePath.h"

@implementation GZLoginState

static GZLoginState *instance = nil;

//@{@"userName": @"", @"sex": @"", @"introduce": @"", @"isReal": @"", @"reputation": @"", @"gunzCoin": @"", @"balance": @""}

-(instancetype)init {
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *path = [GZFilePath getUserInfoPath];
    if([manager fileExistsAtPath:path]) {
        NSData *dat = [NSData dataWithContentsOfFile:[GZFilePath getUserInfoPath]];
        self.userInfo = [NSKeyedUnarchiver unarchiveObjectWithData:dat];
    }
    else {
        self.userInfo = [[NSMutableDictionary alloc] init];
        [self.userInfo setObject:@"" forKey:@"loginAccount"];
        [self.userInfo setObject:@"" forKey:@"password"];
        [self.userInfo setObject:@"" forKey:@"userName"];
//        [self.userInfo setObject:@"" forKey:@"userID"];
        [self.userInfo setObject:@"" forKey:@"sex"];
        [self.userInfo setObject:@"" forKey:@"introduce"];
        [self.userInfo setObject:@"" forKey:@"isReal"];
        [self.userInfo setObject:@"" forKey:@"reputation"];
        [self.userInfo setObject:@"" forKey:@"gunzCoin"];
        [self.userInfo setObject:@"" forKey:@"balance"];
        [self.userInfo setObject:@"" forKey:@"phone"];
        [self.userInfo setObject:@"" forKey:@"email"];
        [self.userInfo setObject:@"" forKey:@"rememberPassword"];
        [self.userInfo setObject:@"" forKey:@"autoLogin"];
    }
    return self;
}

+(GZLoginState*)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if(instance == nil) {
            instance = [[self alloc] init];
        }
    });
    return instance;
}

+(void)setCurrentLoginUserID:(NSString*)userID {
    [[NSUserDefaults standardUserDefaults] setObject:userID forKey:@"currentUserID"];
}

+(NSString*)getCurrentLoginUserID {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"currentUserID"];
}

+(void)setLoginState:(BOOL)state {
    [[NSUserDefaults standardUserDefaults] setBool:state forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+(BOOL)isLogin {
    BOOL state = [[[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"] boolValue];
    return state;
}

+(BOOL)createUserDucomentByUserID:(NSString *)userID {
    NSString *basePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0];
    NSString *baseUserPath = [NSString stringWithFormat:@"%@/%@", basePath, userID];
    NSFileManager *manager = [NSFileManager defaultManager];
    if(![manager fileExistsAtPath:baseUserPath]) {
        BOOL createSuccess = [manager createDirectoryAtPath:baseUserPath withIntermediateDirectories:true attributes:nil error:nil];
        if(createSuccess) {
//            NSArray *userDataType = @[GZUserLightPath, GZUserQuestionPath, GZUserAudioPath, GZUserVideoPath, GZUserImagePath];
            NSArray *userDataType = [GZConst getDataPathList];
            
            for(NSString *dataType in userDataType) {
                NSString *dataTypePath = [NSString stringWithFormat:@"/%@", dataType];
                if(![manager fileExistsAtPath:dataTypePath]) {
                    [manager createDirectoryAtPath:dataTypePath withIntermediateDirectories:true attributes:nil error:nil];
                }
            }
            
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}

-(void)updateUserInfo {
    //发送数据到服务器
    //更改本地磁盘数据
    NSData *info = [NSKeyedArchiver archivedDataWithRootObject:self.userInfo];
    [info writeToFile:[GZFilePath getUserInfoPath] atomically:true];
}

-(void)setLoginAccount:(NSString *)aAccount {
    self.userInfo[@"loginAccount"] = aAccount;
}

-(void)setPassword:(NSString *)aPassword {
    self.userInfo[@"password"] = aPassword;
}

-(void)setUserName:(NSString*)aName {
    self.userInfo[@"userName"] = aName;
}

-(void)setUserID:(NSString *)aUserID {
    self.userInfo[@"userID"] = aUserID;
}

-(void)setUserSex:(NSString*)aSex {
    self.userInfo[@"sex"] = aSex;
}

-(void)setIntroduce:(NSString*)aIntro {
    self.userInfo[@"introduce"] = aIntro;
}

-(void)setIsReal:(NSString*)aReal {
    self.userInfo[@"isReal"] = aReal;
}

-(void)setReputation:(NSString*)aReputation {
    self.userInfo[@"reputation"] = aReputation;
}

-(void)setGunzCoin:(NSString*)aCoin {
    self.userInfo[@"gunzCoin"] = aCoin;
}

-(void)setBalance:(NSString*)aBalance {
    self.userInfo[@"balance"] = aBalance;
}

-(void)setPhone:(NSString *)aPhone {
    self.userInfo[@"phone"] = aPhone;
}

-(void)setEmail:(NSString *)aEmail {
    self.userInfo[@"email"] = aEmail;
}

-(void)setRememberPasswordState:(BOOL)state {
    if(state) {
        self.userInfo[@"rememberPassword"] = @"true";
    }
    else {
        self.userInfo[@"rememberPassword"] = @"false";
    }
}

-(void)setAutoLoginState:(BOOL)state {
    if(state) {
        self.userInfo[@"autoLogin"] = @"true";
    }
    else {
        self.userInfo[@"autoLogin"] = @"false";
    }
}

-(NSString*)getLoginAccount {
    return self.userInfo[@"loginAccount"];
}

-(NSString*)getPassword {
    return self.userInfo[@"password"];
}

-(NSString*)getUserName {
    return self.userInfo[@"userName"];
}

-(NSString*)getSex {
    return self.userInfo[@"sex"];
}

-(NSString*)getIntroduce {
    return self.userInfo[@"introduce"];
}

-(NSString*)getIsReal {
    return self.userInfo[@"isReal"];
}

-(NSString*)getReputation {
    return self.userInfo[@"reputation"];
}

-(NSString*)getGunzCoin {
    return self.userInfo[@"gunzCoin"];
}

-(NSString*)getBalance {
    return self.userInfo[@"balance"];
}

-(NSString*)getPhone {
    return self.userInfo[@"phone"];
}

-(NSString*)getEmail {
    return self.userInfo[@"email"];
}

-(BOOL)getRememberPasswordState {
    if([self.userInfo[@"rememberPassword"]  isEqual: @"true"]) {
        return true;
    }
    else {
        return false;
    }
}

-(BOOL)getAutoLoginState {
    if([self.userInfo[@"autoLogin"] isEqual: @"true"]) {
        return true;
    }
    else {
        return false;
    }
}

//@{@"userName": @"", @"sex": @"", @"introduce": @"", @"isReal": @"", @"reputation": @"", @"gunzCoin": @"", @"balance": @""}

@end





